<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('公司基本信息') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('公司全称')" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          :placeholder="$t('公司全称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('公司简称')" prop="companyShortName">
        <el-input
          v-model="queryParams.companyShortName"
          :placeholder="$t('公司简称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('一级行业')" prop="industryFirst">
        <el-select v-model="queryParams.industryFirst" filterable :placeholder="$t('一级行业')">
          <el-option
            v-for="si in industryFirstOptions"
            :key="si.id"
            :label="$t(si.name)"
            :value="si.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:base:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:base:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="companyBaseList" border>
      <el-table-column :label="$t('公司全称')" prop="companyName" :show-overflow-tooltip="true" fixed="left" min-width="250">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/company/base/companyBaseHome',query:{companyId:scope.row.id}}"
            class="link_a"
          >{{ scope.row.companyName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('公司简称')" align="center" prop="companyShortName" show-overflow-tooltip min-width="250" />
      <el-table-column :label="$t('法人代表')" align="center" prop="legalPerson" show-overflow-tooltip min-width="100" />
      <el-table-column :label="$t('一级行业')" align="center" prop="industryFirstLabel" show-overflow-tooltip min-width="160">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.industryFirstLabel) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('二级行业')" align="center" prop="industrySecondLabel" show-overflow-tooltip min-width="160">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.industrySecondLabel) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('注册省')" align="center" prop="provinceRegisteredLabel" show-overflow-tooltip min-width="90">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.provinceRegisteredLabel) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('注册市')" align="center" prop="cityRegisteredLabel" show-overflow-tooltip min-width="90">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.cityRegisteredLabel) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('注册资本')+ $t('金额单位')" align="center" show-overflow-tooltip prop="capitalRegistered" min-width="110">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.capitalRegistered, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('注册资本币种')" align="center" prop="currency" show-overflow-tooltip width="110" :formatter="currencyFormat" />
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" min-width="220">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:client:view']"
            size="small"
            type="text"
            icon="el-icon-link"
            @click="relationHandle(scope.row)"
          >{{ $t('合作关系') }}</el-button>
          <el-button
            v-hasPermi="['company:base:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['company:base:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司基本信息对话框 -->
    <el-dialog :title="$t('公司基本信息')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" :custom-class="companyBaseFormFlag ? 'magicalForm' : 'dialogForm'" append-to-body>
      <Form v-if="companyBaseFormFlag" ref="form" :form-id-prop="formId" />
      <companyBaseForm v-else ref="companyBaseForm" />
    </el-dialog>
    <!-- 关系数据对话框 -->
    <el-dialog :title="$t('合作关系')" :visible.sync="relationFormOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <company-relation-tabs ref="companyRelationTabs" />
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyBase, delCompanyBase } from '@/api/company/base/companyBase'
import companyBaseForm from './companyBaseForm.vue'
import { allFirstList } from '@/api/system/industry/sysIndustry'
import CompanyRelationTabs from '@/views/company/base/companyRelationTabs'
import { getConfigKey } from '@/api/system/config'
import Form from '@/views/dev/form/base/form'

export default {
  components: {
    CompanyRelationTabs,
    companyBaseForm,
    Form
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 公司基本信息表格数据
          companyBaseList: [],
          // 是否显示弹出层
          formOpen: false,
          // 注册资本币种字典
          currencyOptions: [],
          // 实收资本币种字典
          actualCurrencyOptions: [],
          // 一级行业
          industryFirstOptions: [],
          // 查询参数
          queryParams: {
              companyName: undefined,
              companyShortName: undefined,
              industryFirst: undefined,
              industrySecond: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 是否能删除
          whetherDel: false,
          // 关系数据弹出窗口
          relationFormOpen: false,
          // 表单参数
          form: {},
          // 表单校验
          rules: {
            companyName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            companyShortName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            industryFirst: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            industrySecond: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            provinceRegistered: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            cityRegistered: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
          },
          industryFirstLabel: '',
          industrySecondLabel: '',
          // 设计表单ID
          formId: this.$route.query.formId,
          // 是否启用设计表单
          companyBaseFormFlag: false
      }
  },
  created() {
      this.$nextTick(() => {
        getConfigKey('company_base_form').then(res => {
          this.companyBaseFormFlag = JSON.parse(res.msg)
        })
      })
      // 获取所有的一级行业集合
      allFirstList().then(response => {
        this.industryFirstOptions = response.data
      })
      this.getDicts('currency').then(response => {
          this.currencyOptions = response.data
          this.actualCurrencyOptions = response.data
      })
      this.getList()
  },
  methods: {
    /** 查询公司基本信息列表 */
    getList() {
        this.loading = true
        listCompanyBase(this.queryParams).then(response => {
            this.companyBaseList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 注册资本币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 实收资本币种字典翻译
    actualCurrencyFormat(row) {
      return this.selectDictLabel(this.actualCurrencyOptions, row.actualCurrency)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyName: undefined,
            companyShortName: undefined,
            creditCode: undefined,
            legalPerson: undefined,
            setDate: undefined,
            industryFirst: undefined,
            industrySecond: undefined,
            provinceRegistered: undefined,
            cityRegistered: undefined,
            detailsRegistered: undefined,
            capitalRegistered: undefined,
            currency: undefined,
            actualCapital: undefined,
            actualCurrency: undefined,
            logoInfo: undefined,
            tycId: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            remark: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
          var data = {}
          if (this.companyBaseFormFlag) {
            data.formId = this.formId
            this.$refs.form.init(data)
          } else {
            this.$refs.companyBaseForm.init(null)
            // this.$refs.companyBaseForm.openLocation = 'companyList'
          }
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
          if (this.companyBaseFormFlag) {
            this.$refs.form.init(row)
          } else {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyBaseForm.init(row)
            // this.$refs.companyBaseForm.openLocation = 'companyList'
          }
        })
    },
    /** 删除按钮操作 */
    deleteHandle: async function(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
          return delCompanyBase(row.id)
        }).then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/company/companyBase/export', this.queryParams)
    },
    // 查看当前公司的关系数据
    relationHandle(row) {
      this.relationFormOpen = true
      this.$nextTick(() => {
        this.$refs.companyRelationTabs.initData(row.id)
        // this.$refs.companyRelationTabs.initData()
      })
    },
    cancel() {
      this.formOpen = false
      this.getList()
    }
  }
}
</script>
